module ComponentsHelper
  LOADER = '/images/active_scaffold/default/indicator.gif'

  def template_form_column(record, input_name)
    url = "'/templates/select/#{record.template_id}?tags=' + $('template_tags').value"
    ajax = "new Ajax.Request(#{url}, " +
      "{ method: 'get', " +
      "  onLoading: function(req) {$('loader_template').show();}," +
      "  onLoaded: function(req) {$('loader_template').hide();}," +
      "  onSuccess: function(transport) {" +
      "    $('images').hide();" +
      "    $('templates').show();" +
      "    $('templates').innerHTML = transport.responseText;" +
      "  }});" 
    inplace_select = (record.template) ? select_template_item(record.template, true) : ""
    text_field_tag('record[tag_list]', '关键字', :id => "template_tags" ) + 
      content_tag("a", "选模板", :href => "#", :onclick => ajax + "return false;") + " " +
      content_tag("img", "", :src => LOADER, :id => "loader_template", :style => "display:none") +
      content_tag("div", '', :class => "sidebarSelect", :id => "templates") +
      content_tag("div", inplace_select, :class => "inplaceSelect", :id => "templates")
  end

  def images_form_column(record, input_name)
    url = "'/images/select/#{record.images.collect{|img|img.id}.join("_")}?tags=' + $('image_tags').value"
    ajax = "new Ajax.Request(#{url}, " +
      "{ method: 'get', " +
      "  onLoading: function(req) {$('loader_image').show();}," +
      "  onLoaded: function(req) {$('loader_image').hide();}," +
      "  onSuccess: function(transport) {" +
      "    $('images').show();" +
      "    $('templates').hide();" +
      "    $('images').innerHTML = transport.responseText;" +
      "  }});" 
    inplace_select = (record.images) ? select_image_items(record.images, true) : "" 
    #@selections.include?(image.id.to_s)
    text_field_tag('record[tag_list]', '关键字', :id => "image_tags" ) + 
      content_tag("a", "选图片", :href => "#", :onclick => ajax + "return false;") + " " +
      content_tag("img", "", :src => LOADER, :id => "loader_image", :style => "display:none") +
      content_tag("div", '', :class => "sidebarSelect", :id => "images") +
      content_tag("div", inplace_select, :class => "inplaceSelect", :id => "images")
  end

  def bak4_template_form_column(record, input_name)
    html = Template.find(:all).collect do |template|
      radio_button_tag(input_name + "[id]", template.id, record.template_id == template.id) + 
        template_preview(template, false)
    end.join("<br />\n")

    link_to_function("选模板", "$$('#templates')[0].show();$$('#images')[0].hide();") +
    content_tag("div", html, :class => "sidebarSelect", :id => "templates")
  end

  def bak4_images_form_column(record, input_name)
    html = Image.find(:all).collect do |image|
      check_box_tag(input_name + "[#{image.id}][id]", image.id, record.images.include?(image)) + 
        image_preview(image, false)
    end.join("<br />\n")
    
    link_to_function("选图片", "$$('#images')[0].show();$$('#templates')[0].hide();") +
    content_tag("div", html, :class => "sidebarSelect", :id => "images")
  end

  def bak3_template_form_column(record, input_name)
    html = radio_button_tag(input_name + "[id]", nil, record.template.nil?) + 
      image_tag("/template/preview/empty.png") + "<br />"
    base_templates = Template.find_tagged_with("基本")
    #html << "基本模板：<br />" unless base_templates.empty?
    html << base_templates.collect do |template|
      radio_button_tag(input_name + "[id]", template.id, record.template_id == template.id) + 
        template_preview(template)
    end.join("<br />")
    related_templates = Template.find_tagged_with(record.tag_list)
    #html << "相关模板：<br />" unless related_templates.empty?
    html << related_templates.collect do |template|
      radio_button_tag(input_name + "[id]", template.id, record.template_id == template.id) + 
        template_preview(template)
    end.join("<br />")
  end

  def bak3_images_form_column(record, input_name)
    html = ""
    related_images = Image.find_tagged_with(record.tag_list)
    html << related_images.collect do |image|
      check_box_tag(input_name + "[#{image.id}][id]", image.id, record.images.include?(image)) + 
        image_preview(image)
    end.join("<br />")
  end

  def bak2_template_form_column(record, input_name)
    select_tag input_name + "[id]", "<option>---</option>" +
      '<optgroup label="基本模板">' + 
      options_from_collection_for_select(Template.find_tagged_with("基本"), :id, :title) +
      '</optgroup>' +
      '<optgroup label="相关模板">' + 
      options_from_collection_for_select(Template.find_tagged_with(record.tag_list), :id, :title) +
      '</optgroup>' , 
      :onclick => "$('template_preview').src=#{url_for_file}"
  end

  def bak_template_form_column(record, input_name)
    html = ""
    html << hidden_field_tag(input_name)
    template_preview_file = (record.template) ? url_for_file_column(record.template, :preview) :
      "/template/preview/empty.png"
    html << image_tag(template_preview_file, :id => :template_preview, :style => "border:1px solid #1F7F00;")
    html << link_to_remote("选取", :update => :browse_templates, 
                           :url => {:controller => :templates, :action => :browse, :tag_list => record.tag_list},
                          :complete => "this.hide();")
    html << "/"
    html << link_to_function("切换", "$('browse_templates').toggle();")
    html << tag('div', :id => :browse_templates)
  end
end
